package com.yqs.maven_jdbc.dao.impl;

import com.yqs.maven_jdbc.dao.EmployeeDao;
import com.yqs.maven_jdbc.entity.Employee;
import com.yqs.maven_jdbc.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class EmployeeDaoImpl implements EmployeeDao {

    @Override
    public List<Employee> getAllEmp() {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            List<Employee> list = new ArrayList<>();
            String sql = "SELECT * FROM t_emp";
            conn = JDBCUtils.getConn();
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                Employee emp = new Employee();
                emp.setEmpId(rs.getInt("emp_id"));
                emp.setEmpName(rs.getString("emp_name"));
                emp.setEmpSalary(rs.getDouble("emp_salary"));
                emp.setEmpAge(rs.getInt("emp_age"));
                list.add(emp);
            }
            return list;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtils.closeAll(conn, pstmt, rs);
        }
    }
}